# coding=utf-8


import simplejson as json
from django.core.urlresolvers import reverse
from django.test import TestCase
from rest_framework.test import APIClient

from sms.response_content import PHONE_NUMBER_LESS_THAN_11
from .models import AuthSMS


def generate_authsms(auth_dict):
    auth = AuthSMS.objects.create(**auth_dict)
    return auth


class AuthSMSTest(TestCase):
    def setUp(self):
        self.client = APIClient()

    def post_phone_to_get_auth_code_succeed(self):
        data = {
            "flag": "register",
            "phone_number": "13220858781",
        }

        res = self.client.post(path=reverse("auth_sms_view"), data=data)
        print
        res
        self.assertEqual(res.status_code, 200)
        self.assertTrue(AuthSMS.objects.filter(phone_number=data["phone_number"]).exists())

    def test_post_phone_to_get_auth_code_with_less_digits(self):
        data = {"phone_number": "1322085878"}
        res = self.client.post(path=reverse("auth_sms_view"), data=data)
        print
        res
        self.assertEqual(res.status_code, 400)
        res = json.loads(res.content)
        self.assertEqual(res["content"], PHONE_NUMBER_LESS_THAN_11)

    def test_post_phone_to_get_auth_code_with_str(self):
        data = {"phone_number": "13220858781*"}
        res = self.client.post(path=reverse("auth_sms_view"), data=data)
        print
        res
        self.assertEqual(res.status_code, 400)
        res = json.loads(res.content)
        self.assertEqual(res["content"], PHONE_NUMBER_LESS_THAN_11)

    def test_post_phone_to_get_auth_code_with_fault_num(self):
        data = {"phone_number": "1322085878122"}
        res = self.client.post(path=reverse("auth_sms_view"), data=data)
        print
        res
        self.assertEqual(res.status_code, 400)
        res = json.loads(res.content)
        self.assertEqual(res["content"], PHONE_NUMBER_LESS_THAN_11)
